package com.shxy.cloudclassroom.controller.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shxy.cloudclassroom.entity.pojo.LearningResource;
import com.shxy.cloudclassroom.entity.pojo.Major;
import com.shxy.cloudclassroom.entity.pojo.User;
import com.shxy.cloudclassroom.entity.vo.ResourceDetailVO;
import com.shxy.cloudclassroom.service.LearningResourceService;
import com.shxy.cloudclassroom.service.MajorService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static com.shxy.cloudclassroom.utils.SystemConstant.*;


/**
 * @Author 吴汇明
 * @School 绥化学院
 * @CreateTime 2025.10.16
 * 学习资源
 */
@Controller
@RequestMapping("/resource")
public class LearningResourceController {

    @Resource
    private LearningResourceService learningResourceService;

    @Resource
    private MajorService majorService;

    /**
     * 资源列表页面
     */
    @GetMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer size,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) Integer majorId,
                       Model model, HttpSession session) {
        //分页查询
        Page<LearningResource> resourcePage = new Page<>(page, size);
        LambdaQueryWrapper<LearningResource> wrapper = new LambdaQueryWrapper<>();
        if (keyword != null) {
            wrapper.like(LearningResource::getTitle, keyword);
        }
        if (majorId != null) {
            wrapper.eq(LearningResource::getMajorId, majorId);
        }
        wrapper.orderByDesc(LearningResource::getDownloadCount);
        List<LearningResource> list = learningResourceService.getResourceList(resourcePage, wrapper);
        List<Major> majors = majorService.list();
        model.addAttribute(HELPER_PAGE, resourcePage);
        model.addAttribute(KEYWORD, keyword);
        model.addAttribute(MAJOR_ID, majorId);
        model.addAttribute(MAJOR_LIST, majors);
        model.addAttribute(RESOURCE_LIST, list);
        return "resource/list";
    }

    /**
     * 上传资源页面
     */
    @GetMapping("/upload")
    public String uploadPage(Model model) {
        return "resource/upload";
    }

    /**
     * 上传资源
     */
    @PostMapping("/upload")
    public String uploadResource(LearningResource resource,
                                 @RequestParam("file") MultipartFile file,
                                 HttpSession session,
                                 Model model) {
        Map<String, Object> result = learningResourceService.uploadResource(resource, file, session);
        if (!(boolean) result.get("success")) {
            model.addAttribute(UPLOAD_MESSAGE, result.get("message"));
            if ("请先登录".equals(result.get("message"))) {
                return "redirect:/login";
            }
            return "resource/upload";
        } else {
            model.addAttribute(UPLOAD_MESSAGE, result.get("message"));
            return "redirect:/resource/" + result.get("resourceId");
        }
    }


    /**
     * 下载资源
     */
    @GetMapping("/download/{id}")
    public void downloadResource(@PathVariable Integer id, HttpServletResponse response
            , HttpSession session) {
        learningResourceService.downloadResource(id, response, session);
    }

    /**
     * 查看资源详情
     */
    @GetMapping("/{id}")
    public String detail(@PathVariable Integer id, Model model, HttpSession session) {
        ResourceDetailVO resourceDetailVO = learningResourceService.getResourceDetail(id, session);
        if (resourceDetailVO == null) {
            return "redirect:/resource/list";
        }
        model.addAttribute(RESOURCE, resourceDetailVO);
        model.addAttribute(MAJOR, resourceDetailVO);
        return "/resource/detail";
    }

    @GetMapping("/isAvailable/{resourceId}/{status}")
    @ResponseBody
    public Map<String, Object> setIsAvailable(@PathVariable("resourceId") Long id
            , @PathVariable("status") Integer status) {
        return learningResourceService.setIsAvailable(id, status);
    }
}